import { reactive } from 'vue'
import { useRoute } from 'vue-router'
import { TY, getQueryObject } from '@/utils'

/**
 * 快速获取参数
 * @param {String|Array<String>} queryKey 参数Key
 * @returns 参数
 *
 * const { ... } = useQuery()
 *
 * const { id } = useQuery('id')
 *
 * const { id, lijunhua } = useQuery(['id', 'lijunhua'])
 */
export function useQuery(queryKey) {
  if (!queryKey) return getQueryObject()

  const route = useRoute()
  let query = reactive({})

  const getQuery = (key) => {
    if (!TY.isString(key)) {
      query[key] = ''
      return
    }

    query[key] = route.query[key]
  }

  if (TY.isArray(queryKey)) {
    queryKey.forEach((key) => getQuery(key))
  } else {
    getQuery(queryKey)
  }

  return query
}
